﻿using CodeNotes.BLL;
using CodeNotes.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace MvcCodeNotes.Controllers
{
    public class ApiTodoController : ApiController
    {
         [HttpPost]
        public CW_User Login(string LoginName, string Password)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            CW_User user = new CW_User();
            try
            {
                user = new CW_UserLogic().GetModelByLoginnamePassword(LoginName, Password);
                if (user != null)
                {
                    dic.Add("state", "success");
                    dic.Add("uid", user.Id);
                }
                else
                {
                    dic.Add("state", "error");
                    dic.Add("msg", "用户名或密码不对");
                }
            }
            catch (Exception e)
            {
                dic.Add("state", "error");
                dic.Add("msg", e.Message.ToString());
            }

            return user;
        }

        // GET api/apitodo
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/apitodo/5
        public string Get(int id)
        {
            return "value=" + id;
        }

        
        public string Get(string callback, string LoginName, string Password)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            List<CW_User> user = new CW_UserLogic().GetModelList("");

            string result = String.Format("{0}({1})", callback, Serializer.Serialize(user));

            return result;
        }

        public string Get(int uid, string callback)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            CW_Todo oldModel = new CW_TodoLogic().GetModelByUserId(uid);

            string result = "";
            if (oldModel != null)
                result = oldModel.JsonContent;

            return result;
        }

        // POST api/apitodo
        public string Post([FromBody]string value)
        {
            return "Post=value" + value;
        }

        // PUT api/apitodo/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/apitodo/5
        public void Delete(int id)
        {
        }
    }
}
